package com.tzt.controller.agent;

import com.tzt.common.BaseController;
import com.tzt.contents.ErrorCodeConstant;
import com.tzt.exception.InnerException;
import com.tzt.service.AgentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 代理商Controller层
 *
 * @author Administrator
 * @auterAdministratorharryleng
 * @Date:2018/4/2
 */
@RestController
public class AgentController extends BaseController
{
    @Resource
    private AgentService agentService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 获取代理商集合
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param page 页数
     * @param size 大小
     * @param keyword 关键字（手机号；名称；id）
     * @param loginRoleId 登录角色id
     * @param menuId 菜单id
     * @return
     */
    @RequestMapping (value = "/api/getAgentList", method = RequestMethod.POST)
    public Map<String, Object> getAgentList(String shopUserId, String token, String page, String size, String keyword, String loginRoleId,
            String menuId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            map=agentService.getAgentList(shopUserId, token, page, size, keyword, loginRoleId, menuId);
        }
        catch (NumberFormatException e)
        {
            logger.error("获取代理商集合：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("获取代理商集合：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("获取代理商集合：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 代理商绑定终端
     * @param shopUserId 商家登录id
     * @param token 验签
     * @param equipmentList 设备信息集合
     * @param agentId 代理商id
     * @return
     */
    @RequestMapping (value = "/api/bindAgentTerminal", method = RequestMethod.POST)
    public Map<String, Object> bindAgentTerminal(String shopUserId, String token, String equipmentList, String agentId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
           agentService.bindAgentTerminal(shopUserId,token,equipmentList,agentId);
        }
        catch (NumberFormatException e)
        {
            logger.error("绑定代理商终端：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("绑定代理商终端：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("绑定代理商终端：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 代理商解绑终端
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param serialNumber 序列号
     * @param terminalId 终端id
     * @param agentId 代理商id
     * @return
     */
    @RequestMapping (value = "/api/unBindAgentTerminal", method = RequestMethod.POST)
    public Map<String, Object> unBindAgentTerminal(String shopUserId, String token, String serialNumber, String terminalId,String agentId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            agentService.unBindAgentTerminal(shopUserId,token,serialNumber, terminalId,agentId);
        }
        catch (NumberFormatException e)
        {
            logger.error("解绑代理商终端：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("解绑代理商终端：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("解绑代理商终端：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 终端详情
     * @param page 页数
     * @param size 大小
     * @param keyword 关键字（序列号；终端id；名称）
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param agentId 代理商id
     * @param name 名称
     * @param loginRoleId 登录角色id
     * @return
     */
    @RequestMapping (value = "/api/getAgentTerminal", method = RequestMethod.POST)
    public Map<String, Object> getAgentTerminal(String page, String size, String keyword, String shopUserId, String token, String agentId,
            String name,String loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            map=agentService.getAgentTerminal(page,size,keyword,shopUserId,
                    token,agentId,name,loginRoleId);
        }
        catch (NumberFormatException e)
        {
            logger.error("查询代理商终端详情：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询代理商终端详情：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("查询代理商终端详情：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 查询代理商未绑定的终端
     * @param page 页数
     * @param size 大小
     * @param keyword 关键字（序列号；终端id；名称）
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param agentId 代理商id
     * @param name 名称
     * @param loginRoleId 登录角色id
     * @return
     */
    @RequestMapping (value = "/api/getUnBindAgentTerminal", method = RequestMethod.POST)
    public Map<String, Object> getUnBindAgentTerminal(String page, String size, String keyword, String shopUserId, String token,
            String agentId,String name,String loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            map=agentService.getUnBindAgentTerminal(page,size,keyword,
                    shopUserId,token,agentId,name,loginRoleId);
        }
        catch (NumberFormatException e)
        {
            logger.error("查询未绑定代理商的终端：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询未绑定代理商的终端：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("查询未绑定代理商的终端：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 查询代理商商品
     * @param page 页数
     * @param size 大小
     * @param keyword 关键字（名称；条形码）
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param agentId 代理商id
     * @return
     */
    @RequestMapping (value = "/api/getAgentProduct", method = RequestMethod.POST)
    public Map<String, Object> getAgentProduct(String page, String size, String keyword, String shopUserId, String token,
            String agentId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            map=agentService.getAgentProduct(page,size,keyword,shopUserId,
                    token,agentId);
        }
        catch (NumberFormatException e)
        {
            logger.error("查询代理商商品：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询代理商商品：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("查询代理商商品：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 更改商品售价
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param price 价格
     * @param productId 商品id
     * @param loginRoleId 登录角色id
     * @return
     */
    @RequestMapping (value = "/api/updateProductPrice", method = RequestMethod.POST)
    public Map<String, Object> updateProductPrice(String shopUserId, String token, String price,String productId,String loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            agentService.updateProductPrice(shopUserId, token, price, productId, loginRoleId);
        }
        catch (NumberFormatException e)
        {
            logger.error("更改商品售价失败：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("更改商品售价失败：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("更改商品售价失败：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 更改商品成本价
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param cost 成本价
     * @param productId 商品id
     * @param loginRoleId 登录角色id
     * @return
     */
    @RequestMapping (value = "/api/updateProductCost", method = RequestMethod.POST)
    public Map<String, Object> updateProductCost(String shopUserId, String token,String cost,String productId,String loginRoleId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            agentService.updateProductCost(shopUserId, token, cost, productId, loginRoleId);
        }
        catch (NumberFormatException e)
        {
            logger.error("更改商品成本价失败：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("更改商品成本价失败：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("更改商品成本价失败：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }
}
